/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frc2856.robot;

import com.sun.squawk.microedition.io.FileConnection;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;

/**
 *
 * @author Dustin
 */
public class Autonomous {
    //why my stuff not on here? Shankar.
    int x = 0, rounds = 0;
    boolean turn = false;
    
    //References to HolonomicDrive and Shooter
    HolonomicDrive holdri;
    Shooter shoot;
    
    //new stuff
    public FileConnection fc;
    public boolean usable = true;
    //end new stuff
    
    public Autonomous(HolonomicDrive hd, Shooter s){
        holdri = hd;
        shoot = s;
    }
    
    
    //new stuff
    
    public void command(String[] c){
        if(x >= c.length)
            return;
        
        String temp = c[x];
        
        if(rounds == 0){
            if(temp.charAt(0) == 't'){//translate
                turn = false;
                //leave directions (N, S, E, W, NE, NW, SE, SW) till later
                if(isInt(temp.substring(2)))
                    rounds = Integer.parseInt(temp.substring(2));
            }
            else if(temp.charAt(0) == 'r'){//rotate
                turn = true;
                if(isInt(temp.substring(1)))
                    rounds = Integer.parseInt(temp.substring(1));
            }
            else if(temp.charAt(0) == 's'){//shoot
                shoot.shoot(true);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                shoot.unshoot();
            }
            x++;
        }
        else{
            if(turn) {
                holdri.drive(0f, 0f, 0.5f);
            }
                //System.out.print("put in turn statement");
            else {
                holdri.drive(0.5f, 1f, 0f);
            }
                //System.out.print("put in a transletion ststion");
            rounds--;
        }
    }
    
    public boolean isInt(String s){
        boolean isInt = true;
        try{
            Integer.parseInt(s);
        }catch(NumberFormatException nfe){
            isInt = false;
        }
        
        return isInt;
    }
    
    /**
     * finds the file name fileName in the robotMap and interprets the file
     * @return a String of the file
     */
    public String readFromFile(){
        
        
        try {
            fc = (FileConnection) Connector.open("file:////" + ""/*Maybe put file name in RobotMap*/);

            if (!fc.exists()) 
            {
                System.out.println("AUTO file doesn't exist! FATAL ERROR: preparing self for oblivion");
                usable = false;
            }
        } catch (Exception e) //problem with this?
        {
            e.printStackTrace();
        }
        
        
        String output = null;
        if (usable)// if auto.txt exists
        {
            try {
                InputStream is = fc.openInputStream();
                byte b[] = new byte[1024];
                int length = is.read(b, 0, 1024);
                output = new String(b, 0, length);
                is.close();
                fc.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return output;
    }
    //end new stuff
}
